package com.coding.service.impl;


import com.coding.base.pojo.BaseModel;
import com.coding.dao.PoemDao;
import com.coding.pojo.PoemBasic;
import com.coding.pojo.Poempk;
import com.google.gson.Gson;

import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.SocketException;
import java.util.Iterator;

/**
 * @author Administrator
 */
public class ServerimplThread implements Runnable {
    public PoemDao poemDao;


    Socket socket = null;
    BufferedReader br = null;
    BufferedWriter rw;
    ServerSocket serverSocket;



    public ServerimplThread(Socket s, PoemDao poemDao,ServerSocket serverSocket) throws IOException {
        this.socket = s;
        br = new BufferedReader(new InputStreamReader(s.getInputStream(), "utf-8"));
        this.poemDao = poemDao;
    }

    @Override
    public void run() {
        try {
            Poempk content = null;

            while ((content = readFrom()) != null) {
                for (Iterator<Socket> it = PoempkImpl.pkList.iterator(); it.hasNext(); ) {
                    Socket s = it.next();
                    try {
                        OutputStream os = s.getOutputStream();
                        rw = new BufferedWriter(new OutputStreamWriter(s.getOutputStream(), "utf-8"));
                        BaseModel baseModel=new BaseModel();
                        Poempk poempk = content;
                        baseModel.setSucceed("成功获得诗词",poempk);
                        Gson gson = new Gson();
                        String json = gson.toJson(baseModel);
//                        rw.write(json+"\n");
                        //字符串转化byte数组
                        byte[] bytes = (json + "\n").getBytes("utf-8");
                        os.write(bytes);
                    } catch (SocketException e) {
                        e.printStackTrace();
                        it.remove();
                        System.out.println(PoempkImpl.pkList);
                    }
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 定义读取客户端数据的方法--数据流传递
     *
     * @return
     */
    private String readFromClient() {
        try {
            return br.readLine();

        } catch (IOException e) {
            e.printStackTrace();
            PoempkImpl.pkList.remove(socket);
        }
        return null;
    }

    /**
     * 定义读取客户端数据的方法--gson传递
     * 从后端查询诗词信息
     *
     * @return
     */
    private Poempk readFrom() {
        Poempk poempk=new Poempk();
        try {
            String s = br.readLine();
            Gson gson = new Gson();
             poempk = gson.fromJson(s, Poempk.class);
            //开启方法访问后端数据

            if (poemDao!=null&&s!=null) {
                PoemBasic poemBasic = poemDao.selectByText(poempk.getPoem_content());
//                System.out.println(poemBasic.toString());
                if (poemBasic != null) {
                    String poem_title = poemBasic.getPoem().getPoem_name();
                    String poet = poemBasic.getPoet().getPoet_name();
                    poempk.setPoem_title(poem_title);
                    poempk.setPoem_poemer(poet);
                    poempk.setStatus(1);
                    System.out.println("查找到诗的内容" + poempk.getPoem_content());
                }else {
                    poempk.setPoem_title("未查找到诗的内容");
                    poempk.setPoem_poemer("未查找到诗的内容");
                    poempk.setStatus(0);
                }
            }
            return poempk;
        } catch (IOException e) {
            e.printStackTrace();
            PoempkImpl.pkList.remove(socket);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return poempk;
    }
}
